package com.salton123.feature

import android.app.Activity
import androidx.fragment.app.Fragment
import com.gyf.immersionbar.ImmersionBar
import com.salton123.arch.view.IImmersionView

/**
 * User: newSalton@outlook.com
 * Date: 2018/12/25 4:51 PM
 * ModifyTime: 4:51 PM
 * Description:
 */
class ImmersionFeature : IFeature, IImmersionView {
    private var mImmersionBar: ImmersionBar? = null
    private lateinit var host: Any
    fun with(host: Any) {
        if (host is Activity ||
            host is Fragment) {
            this.host = host
        } else {
            throw IllegalArgumentException("host must instanceof activity or fragment")
        }
        this.host = host
    }

    override fun onBind() {
        mImmersionBar = getImmersionBar()
        mImmersionBar!!.init()
    }

    override fun getImmersionBar(): ImmersionBar? {
        if (mImmersionBar == null) {
            if (host is Activity) {
                mImmersionBar = ImmersionBar.with((host as Activity?)!!)
            } else if (host is Fragment) {
                mImmersionBar = ImmersionBar.with((host as Fragment?)!!)
            }
        }
        return mImmersionBar
    }

    override fun onUnBind() {
    }

    override fun darkFont() {
        getImmersionBar()?.statusBarDarkFont(true)?.init()
    }

    override fun lightFont() {
        getImmersionBar()?.statusBarDarkFont(false)?.init()
    }
}